package com.cxrj.zhsq.partyMember.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.partyMember.dao.JobsApplyMapper;
import com.cxrj.zhsq.partyMember.dao.JobsMapper;
import com.cxrj.zhsq.partyMember.entity.Jobs;
import com.cxrj.zhsq.partyMember.entity.JobsApply;
import com.cxrj.zhsq.partyMember.service.PartyMemberJobsService;
import com.cxrj.zhsq.userManage.dao.AppUserMapper;
import com.cxrj.zhsq.userManage.entity.AppUser;
import com.cxrj.zhsq.utils.SqlCondition;


@Service
public class PartyMemberJobsServiceImpl implements PartyMemberJobsService {
	
	@Resource 
	private JobsMapper jobsMapper;
	@Resource
	private JobsApplyMapper jobsApplyMapper;
	@Resource
	private AppUserMapper appUserMapper;
	
	@Override
	public Map<String, Object> selectJobsPage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = SqlCondition.getBaseCondition(request, response);
		//查询条件
		String kod=request.getParameter("kod");
		String kodType=request.getParameter("kodType");
		String sqlStr = " where 1=1 ";
		
		if(kodType != null && !kodType.equals("")){
			sqlStr += "and jobs_state = " + kodType;
		}
		
		if(kod != null && !kod.equals("")){
			//sqlStr += " and ( work_order_title like " + '"' + '%' + kod + '%' + '"'+" or work_order_people like " + '"' + '%' + kod + '%' + '"'+')';
			sqlStr += " and jobs_name like " + '"' + '%' + kod + '%' + '"';
		}
		sqlMap.put("condition",sqlStr);
		
		int count = jobsMapper.selectCount(sqlMap);
		List<Jobs> list = jobsMapper.selectJobsPage(sqlMap);
		resultMap.put("total", count);
		resultMap.put("recordsFiltered", count);
		resultMap.put("rows", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> insertJobs(Jobs jobs, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<>();
		jobs.setJobsId(UUID.randomUUID().toString());
		jobs.setCreateTime(new Date());
		jobs.setJobsState(0);
		int a=jobsMapper.insertSelective(jobs);
		resultMap.put("result", a);
		return resultMap;
	}

	@Override
	public Map<String, Object> updateJobs(Jobs jobs, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<>();
		int a=jobsMapper.updateByPrimaryKeySelective(jobs);
		resultMap.put("result", a);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectJobsById(String jobsId, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<>();
		Jobs jobs=jobsMapper.selectByPrimaryKey(jobsId);
		resultMap.put("result", jobs);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectJobsApply(String jobsId, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<>();
		List<JobsApply> jobsApplies=jobsApplyMapper.selectJobsApply(jobsId);
		resultMap.put("result", jobsApplies);
		return resultMap;
	}

	@Override
	public Map<String, Object> updatePassJobsApply(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<>();
		resultMap.put("result", 0);
		String aId=request.getParameter("aId");
		String jobsId=request.getParameter("jobsId");
		String userId=request.getParameter("userId");
		JobsApply jobsApply=new JobsApply();
		jobsApply.setaId(aId);
		jobsApply.setApplyState(1);
		int a=jobsApplyMapper.updateByPrimaryKeySelective(jobsApply);//通过审核
		Jobs jobs=jobsMapper.selectByPrimaryKey(jobsId);
		String jobsName=jobs.getJobsName();//获取岗位名称
		jobs.setJobsState(1);
		int b=jobsMapper.updateByPrimaryKeySelective(jobs);//岗位已任岗
		AppUser appUser=appUserMapper.selectAppUserById(userId);
		if(appUser.getCommunityName()==null){
			appUser.setCommunityName(jobsName);
		}else{
			appUser.setCommunityName(appUser.getCommunityName()+","+jobsName);
		}
		
		int c=appUserMapper.updateByPrimaryKeySelective(appUser);
		if (a>0 & b>0 & c>0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectJobs(String userId, String dataIndex, String pageCount) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("start", Integer.parseInt(dataIndex));
		sqlMap.put("length", Integer.parseInt(pageCount));
		sqlMap.put("userId", userId);
		List<Jobs> list = jobsMapper.selectJobs(sqlMap);
		for (Jobs jobs : list) {
			sqlMap.put("jobsId", jobs.getJobsId());
			jobs.setIsSign(jobsApplyMapper.selectJobsApplyIsSign(sqlMap));
		}
		resultMap.put("list", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> insertJobsApply(String userId, String jobsId, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AppUser appUser = appUserMapper.selectByPrimaryKey(userId);
		JobsApply jobsApply = new JobsApply();
		jobsApply.setaId(UUID.randomUUID().toString());
		jobsApply.setApplyAddress(appUser.getUserAddress());
		jobsApply.setApplyIdcared(appUser.getUserIdCard());
		jobsApply.setApplyName(appUser.getUserName());
		jobsApply.setApplyState(0);
		jobsApply.setApplyTel(appUser.getUserMobile());
		jobsApply.setApplyTime(new Date());
		jobsApply.setJobsId(jobsId);
		jobsApply.setUserId(userId);
		Integer c = jobsApplyMapper.insertSelective(jobsApply);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectJobsApplyByJobsId(String jobsId) {
		Map<String, Object> resultMap=new HashMap<>();
		JobsApply jobsApply=jobsApplyMapper.selectJobsApplyByJobsId(jobsId);
		resultMap.put("result", jobsApply);
		return resultMap;
	}

	@Override
	public Map<String, Object> insertDirectlyPassJob(String userJob,String jobsId,HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<>();
		resultMap.put("result", 0);
		//String userJob=request.getParameter("userJob");
		//String jobsId=request.getParameter("jobsId");
		
		String[] user=userJob.split(",");
		JobsApply jobsApply=new JobsApply();
		jobsApply.setaId(UUID.randomUUID().toString());
		jobsApply.setUserId(user[0]);
		jobsApply.setApplyName(user[1]);
		jobsApply.setApplyTel(user[2]);
		jobsApply.setApplyIdcared(user[3]);
		jobsApply.setApplyState(1);
		jobsApply.setApplyTime(new Date());
		jobsApply.setJobsId(jobsId);
		int a=jobsApplyMapper.insertSelective(jobsApply);
		Jobs jobs=jobsMapper.selectByPrimaryKey(jobsId);
		String jobsName=jobs.getJobsName();//获取岗位名称
		jobs.setJobsState(1);
		int b=jobsMapper.updateByPrimaryKeySelective(jobs);//岗位已任岗
		AppUser appUser=appUserMapper.selectAppUserById(user[0]);
		if(appUser.getCommunityName()==null){
			appUser.setCommunityName(jobsName);
		}else{
			appUser.setCommunityName(appUser.getCommunityName()+","+jobsName);
		}
		
		int c=appUserMapper.updateByPrimaryKeySelective(appUser);
		if (a>0 & b>0 & c>0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

}
